﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface;
using BusinessObjects;
using DataObjects.AdoNet;
using System.Data;

namespace DataObjects.SqlServer
{
    public class SqlServerRestaurantTableDao : IRestaurantTableDao
    {
        public List<RestaurantTable> GetRestaurantTables()
        {
            string procedures = "Get_All_RestaurantTables";

            return Db.ReadList(procedures, true, Make);
        }
        public List<RestaurantTable> GetRestaurantTablesByPageAndBranch(int currentPage, string restaurantdId)
        {
            string procedures = "Get_Paged_RestaurantTables";

            object[] parms = {  "@WhereClause", " RestaurantID = '" + restaurantdId + "'", 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, Make, parms);
        }
        public List<RestaurantTable> GetRestaurantTablesByPageAndArea(int currentPage, string restaurantAreaId)
        {
            string procedures = "Get_Paged_RestaurantTables";

            object[] parms = {  "@WhereClause", " RestaurantTableID = '" + restaurantAreaId + "'", 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, Make, parms);
        }
        public RestaurantTable GetRestaurantTable(string restaurantTableId)
        {
            string procedures = "Get_All_RestaurantTable_ByID";

            object[] parms = { "@RestaurantTableID", restaurantTableId };
            return Db.Read(procedures, true, Make, parms);
        }
        public int GetTotalPage()
        {
            string procedures = "Get_PageSize_RestaurantTable";

            object[] parms = { "@WhereClause", null };
            return Db.ReadTotalPage(procedures, true, parms);
        }
        public int GetTotalPageByBranch(string restaurantId)
        {
            string procedures = "Get_PageSize_RestaurantTable";

            object[] parms = { "@WhereClause", " WHERE RestaurantID = '" + restaurantId + "'" };
            return Db.ReadTotalPage(procedures, true, parms);
        }
        public int GetTotalPageByArea(string restaurantAreaId)
        {
            string procedures = "Get_PageSize_RestaurantTable";

            object[] parms = { "@WhereClause", " WHERE RestaurantTableID = '" + restaurantAreaId + "'" };
            return Db.ReadTotalPage(procedures, true, parms);
        }
        public void InsertRestaurantTable(RestaurantTable restaurantTable)
        {
            string sql = "Insert_RestaurantTable";

            restaurantTable.RestaurantTableID = Db.Insert(sql, true, Take(restaurantTable));
        }
        public void UpdateRestaurantTable(RestaurantTable restaurantTable)
        {
            string sql = "Update_RestaurantTable";

            restaurantTable.RestaurantTableID = Db.Insert(sql, true, Take(restaurantTable));
        }
        public void DeleteRestaurantTable(RestaurantTable restaurantTable)
        {
            string sql = "Delete_RestaurantTable";

            Db.Update(sql, true, Take(restaurantTable.RestaurantTableID));
        }

        private static Func<IDataReader, RestaurantTable> Make = reader =>
            new RestaurantTable
            {
                RestaurantTableID = reader["RestaurantTableID"].AsString(),
                RestaurantID = reader["RestaurantID"].AsString(),
                RestaurantAreaID = reader["RestaurantAreaID"].AsString(),
                RestaurantTableCode = reader["RestaurantTableCode"].AsString(),
                RestaurantTableName = reader["RestaurantTableName"].AsString(),
                Description = reader["Description"].AsString(),
                MaxPeople = reader["MaxPeople"].AsInt(),
                IsOrder = reader["IsOrder"].AsBool(),
                IsOrderOnline = reader["IsOrderOnline"].AsBool(),
                IsActive = reader["IsActive"].AsBool()
            };
        private object[] Take(RestaurantTable restaurantTable)
        {
            return new object[]  
            {
                "@RestaurantTableID", restaurantTable.RestaurantTableID,
                "@RestaurantID", restaurantTable.RestaurantID,
                "@RestaurantAreaID", restaurantTable.RestaurantAreaID,
                "@RestaurantTableCode", restaurantTable.RestaurantTableCode,
                "@RestaurantTableName", restaurantTable.RestaurantTableName,
                "@Description", restaurantTable.Description,
                "@MaxPeople", restaurantTable.MaxPeople,
                "@IsOrder", restaurantTable.IsOrder,
                "@IsOrderOnline", restaurantTable.IsOrderOnline,
                "@IsActive", restaurantTable.IsActive
            };
        }
        private object[] Take(string restaurantTableId)
        {
            return new object[]  
            {
                "@RestaurantTableID", restaurantTableId,
            };
        }
    }
}
